package com.alex.controller.Cn_Pubmed;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.alex.common.beans.ResultBean;
import com.alex.common.config.IndexConfig;
import com.alex.entity.ArticleEntity;
import com.alex.entity.PageList;
import com.alex.paramVo.ArticleSearchVo;
import com.alex.service.AllQueryBasicService;
import com.alex.service.FilterService;
import com.alex.utils.RedisUtil;
import com.alex.utils.YouDaoTransLate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@RequestMapping("/cn_pubmed")
@RestController
public class Cn_Pubmed_Controller extends AllQueryBasicService {
    @Resource
    private FilterService filterService;

    /**
     * 研究热词
     *
     * @param uuid
     * @return
     * @throws SQLException
     * @throws IOException
     */
    @PostMapping(value = "research_hotspots")
    public ResultBean<Object> research_hotspots(String uuid) throws SQLException, IOException {
        ArticleSearchVo searchVo = (ArticleSearchVo) RedisUtil.getObject(uuid);
        BoolQuery.Builder boolQuery = null;
        try {
            boolQuery = filterService.getBoolQuery(searchVo);
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
        assert boolQuery != null;
        Query query = boolQuery.build()._toQuery();
        List<String> list = Stream.of("id").collect(Collectors.toList());
        List<Hit<ArticleEntity>> hits = null;
        try {
            hits = allQuery(IndexConfig.CORE_INDEX, query, 0, 200, list, ArticleEntity.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        assert hits != null;
        List<ArticleEntity> coventList = coventList(hits);
        List<Integer> ids = coventList.stream().map(ArticleEntity::getId).collect(Collectors.toList());

        return new ResultBean<>(ResearchHotspots.researchHotspots(ids));

    }

    @PostMapping(value = "auth_net_work")
    public ResultBean<Object> auth_net_work(String uuid) throws SQLException, IOException {

        return new ResultBean<>();
//        ArticleSearchVo searchVo = (ArticleSearchVo) RedisUtil.getObject(uuid);
//        BoolQuery.Builder boolQuery = null;
//        try {
//            boolQuery = filterService.getBoolQuery(searchVo);
//        } catch (SQLException | IOException e) {
//            e.printStackTrace();
//        }
//        assert boolQuery != null;
//        Query query = boolQuery.build()._toQuery();
//        List<String> list = Stream.of("id").collect(Collectors.toList());
//        List<Hit<ArticleEntity>> hits = null;
//        try {
//            hits = allQuery(IndexConfig.CORE_INDEX, query, 0, 200, list, ArticleEntity.class);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        assert hits != null;
//        List<ArticleEntity> coventList = coventList(hits);
//        List<Integer> ids = coventList.stream().map(ArticleEntity::getId).collect(Collectors.toList());
//        System.out.println("课题思路助手的首页关系图的 ids___" + ids.size());
//
//        //return new ResultBean<>(null);
//        return new ResultBean<>(AuthNetWork.auth_net_work(ids));

    }


    @GetMapping(value = "waring_article")
    public ResultBean<Object> waring_article(Integer jid, Integer page, Integer limit) {
        List<Integer> pageListIds = checkPage(page, limit);
        BoolQuery.Builder builder = new BoolQuery.Builder();
        Query titleQ = TermQuery.of(i -> i.field("jid").value(jid))._toQuery();
        builder.must(titleQ);
        SearchResponse<ArticleEntity> response = null;
        try {
            response = allQueryRes(IndexConfig.CORE_INDEX, builder.build()._toQuery(), pageListIds.get(0), pageListIds.get(1), null, ArticleEntity.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (null == response) {
            return null;
        }
        List<ArticleEntity> resultList = coventList(response.hits().hits());
        long value = response.hits().total().value();
        System.out.println("命中数量_" + value);
        PageList<ArticleEntity> pageList = new PageList<>();
        pageList.setUuid(null);
        pageList.setTotalElements(value);
        pageList.setList(resultList);
        pageList.setTotalPages((int) (value / limit < 1 ? 1 : (value / limit) + 1));//总页数
        pageList.setCurrentPage(page);//当前页
        pageList.setPageSize(limit);//每页显示条数

        return new ResultBean<>(pageList);
    }

    @PostMapping(value = "translate")
    public ResultBean<Object> translate(String id) {
        List<Entity> query = null;
        try {
            query = Db.use().query("select id, title from "+IndexConfig.db_article+" where id = ?", id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return new ResultBean<>("暂无中文标题");
    }



}
